package com.dldata.devtools30.controller;

import com.dldata.devtools30.cfg.ApplicationHome;
import com.dldata.devtools30.common.EnumOrganization;
import com.dldata.devtools30.entity.DevtModelOrganizationEntity;
import com.dldata.devtools30.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.sql.Timestamp;
import java.util.*;

/**

 */
@Controller
@RequestMapping(value = "/devindex30")
public class DevtIndexController extends DevBase30Controller {


    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @RequestMapping(value="")
    public String indexdev(HttpServletRequest request,
                        HttpServletResponse response,
                        ModelMap model
    ) {
//        SessionUserInfoEntity sessionUserInfoEntityTmp = RightUtil.getCurrentUser(request);
//        model.addAttribute("staffname", sessionUserInfoEntityTmp.getUname());
//        model.addAttribute("loginname", sessionUserInfoEntityTmp.getLname());
//        model.addAttribute("userId", sessionUserInfoEntityTmp.getUid());
//
//        Properties prop = Public.sysConfig;
//        String hospitalName = prop.getProperty("hospitalName", "");
//        String systemName = prop.getProperty("systemName", "");
//        model.addAttribute("hospitalName",hospitalName);
//        model.addAttribute("systemName",systemName);
//        if(!Public.devuser.contains(sessionUserInfoEntityTmp.getLname())){
//            request.setAttribute("errorinfo","权限不足,请重新登录.");
//            response.setStatus(403);
//            response.setContentType("text/html;charset=utf-8");
//            response.setCharacterEncoding("UTF-8");
//            response.addHeader("errorinfo", URLEncoder.encode("权限不足,请重新登录.","UTF-8"));
//            try {
//                response.getWriter().println("权限不足,请重新登录.");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            return null;
//        }
//        int a=1/0;
        List<DevtModelOrganizationEntity> organizationEntityList = commonApiDao.getScrollData(DevtModelOrganizationEntity.class).getResultlist();
        if(organizationEntityList.size()==0){
            DevtModelOrganizationEntity sysCrfFormOrganizationEntity =new DevtModelOrganizationEntity();
            sysCrfFormOrganizationEntity.setName(EnumOrganization.enumOrganization.getName());
            sysCrfFormOrganizationEntity.setCode(EnumOrganization.enumOrganization.getCode());
            sysCrfFormOrganizationEntity.setReleasetime(new Timestamp(System.currentTimeMillis()));
            sysCrfFormOrganizationEntity.setFormversion(DateUtil.formatFromDate(DateUtil.FORMATER_YYYYMMDDHHMMSS,new Date()));
            sysCrfFormOrganizationEntity.setId(UUID.randomUUID().toString());
            commonApiDao.save(sysCrfFormOrganizationEntity);
            model.addAttribute("organizationEntity",sysCrfFormOrganizationEntity);
        }
        if(organizationEntityList.size()>=1){
            DevtModelOrganizationEntity sysCrfFormOrganizationEntity = organizationEntityList.get(0);
            model.addAttribute("organizationEntity",sysCrfFormOrganizationEntity);
        }
    
        model.addAttribute("organizationMsg","");
        if(organizationEntityList.size()>1){
            model.addAttribute("organizationMsg","机构表只能有一行数据,当前机构表有0行或多行数据,请确认！");
        }
    
        String designdatadir= ApplicationHome.getApplicationConfig()+"/designdata";
    
    
        EnumOrganization[] enumOrganizations = EnumOrganization.enumOrganizationList.toArray(new EnumOrganization[EnumOrganization.enumOrganizationList.size()]);
        List<HashMap> hospitalversionList=new ArrayList<>();
        for (int i = 0; i < enumOrganizations.length; i++) {
            EnumOrganization enumOrganization = enumOrganizations[i];
            HashMap hashMap=new HashMap();
            hashMap.put("value",enumOrganization.getName());
            hospitalversionList.add(hashMap);
            
        }
        model.addAttribute("hospitalversionList",hospitalversionList);
        
        return "thymeleaf/devtools30/index/devindex";
    }
}
